package com.candy.control;

import com.candy.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/loginservlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接收到客户端发送过来的请求");
//        设置字符集，处理中文乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        获取前台传递过来的参数,参数对应前端表单的name属性值
       String username= req.getParameter("username");
        String userpwd= req.getParameter("userpwd");
//       调用业务逻辑处理层，判断用户名密码是否匹配
        LoginService lg=new LoginService();
        boolean ok= lg.logined(username,userpwd);
        if(ok==true){
//            若用户名密码匹配成功，则跳转到登录成功的页面
            req.getRequestDispatcher("WEB-INF/welcome.jsp").forward(req,resp);
        }else {
//            若不匹配，则返回登录页面
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }



    }
}
